﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using CalcSharp.Core;

namespace CalcSharp.MDIUI
{
    public partial class paramcomp : Form
    {
        private ListViewItem ret;
        public Dictionary<string, string> autocompitems;
        public string[] cmdtext;

        public paramcomp()
        {
            InitializeComponent();
            this.categoryview();
            this.ret = new ListViewItem("<- Return", 6);
        }

        public void categoryview()
        {
            this.param_items.Clear();
            this.param_items.Groups.Clear();
            ListViewItem param = new ListViewItem("Parameters...", 1);
            ListViewItem file = new ListViewItem("File...", 0);
            ListViewItem help = new ListViewItem("Get Help...", 7);
            ListViewItem history = new ListViewItem("Command History...", 0);
            ListViewItem mem_float = new ListViewItem("Floating Point...", 0);
            ListViewItem mem_matrix = new ListViewItem("Matrix...", 0);
            ListViewItem mem_cplx = new ListViewItem("Complex...", 0);
            ListViewItem mem_set = new ListViewItem("Set...", 0);
            ListViewItem mem_string = new ListViewItem("String...", 0);
            this.param_items.Items.AddRange(new ListViewItem[] {
            param,
            file,
            help,
            history,
            mem_float,
            mem_matrix,
            mem_cplx,
            mem_set,
            mem_string});
        }

        private void drawFloat()
        {
            ListViewItem tmp;
            this.param_items.Clear();
            this.param_items.Groups.Clear();
            this.param_items.Items.Add(ret);
            int i = 0;
            foreach (string key in Program.loaded.Mem.keys)
            {
                tmp = new ListViewItem();
                tmp.Name = "memitem_" + i.ToString();
                tmp.Text = key;
                tmp.ImageIndex = 3;
                this.param_items.Items.Add(tmp);
                i++;
            }
        }

        private void drawMatrix()
        {
            ListViewItem tmp;
            this.param_items.Clear();
            this.param_items.Groups.Clear();
            this.param_items.Items.Add(ret);
            int i = 0;
            foreach (string key in Program.loaded.Mtrx_mem.keys)
            {
                tmp = new ListViewItem();
                tmp.Name = "memitem_" + i.ToString();
                tmp.Text = key;
                tmp.ImageIndex = 5;
                this.param_items.Items.Add(tmp);
                i++;
            }
        }

        private void drawSet()
        {
            ListViewItem tmp;
            this.param_items.Clear();
            this.param_items.Groups.Clear();
            this.param_items.Items.Add(ret);
            int i = 0;
            foreach (string key in Program.loaded.Set_mem.keys)
            {
                tmp = new ListViewItem();
                tmp.Name = "memitem_" + i.ToString();
                tmp.Text = key;
                tmp.ImageIndex = 4;
                this.param_items.Items.Add(tmp);
                i++;
            }
        }

        private void drawCplx()
        {
            ListViewItem tmp;
            this.param_items.Clear();
            this.param_items.Groups.Clear();
            this.param_items.Items.Add(ret);
            int i = 0;
            foreach (string key in Program.loaded.CPLX_mem.keys)
            {
                tmp = new ListViewItem();
                tmp.Name = "memitem_" + i.ToString();
                tmp.Text = key;
                tmp.ImageIndex = 2;
                this.param_items.Items.Add(tmp);
                i++;
            }
        }

        private void drawString()
        {
            ListViewItem tmp;
            this.param_items.Clear();
            this.param_items.Groups.Clear();
            this.param_items.Items.Add(ret);
            int i = 0;
            foreach (string key in Program.loaded.String_mem.keys)
            {
                tmp = new ListViewItem();
                tmp.Name = "memitem_" + i.ToString();
                tmp.Text = key;
                tmp.ImageIndex = 8;
                this.param_items.Items.Add(tmp);
                i++;
            }
        }

        private void drawParamList()
        {
            this.param_items.Clear();
            this.param_items.Groups.Clear();
            this.param_items.Items.Add(ret);
            ListViewItem tmp;
            string[] its;
            if (this.autocompitems != null)
            {
                foreach (string cat in this.autocompitems.Keys)
                {
                    this.param_items.Groups.Add(cat, cat);
                    its = this.autocompitems[cat].Split(';');
                    for (int i = 0; i < its.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(its[i]))
                        {
                            tmp = new ListViewItem();
                            tmp.Name = "item_" + i.ToString();
                            tmp.Text = its[i];
                            tmp.ImageIndex = 2;
                            tmp.Group = this.param_items.Groups[cat];
                            this.param_items.Items.Add(tmp);
                        }
                    }
                }
            }
            this.param_items.ShowGroups = true;
            this.param_items.Sort();

        }

        private void ActivateItem()
        {
            if (param_items.SelectedItems != null && param_items.SelectedItems.Count > 0)
            {
                switch (param_items.SelectedItems[0].Text)
                {
                    case "Floating Point...":
                        this.drawFloat();
                        break;
                    case "Matrix...":
                        this.drawMatrix();
                        break;
                    case "Complex...":
                        this.drawCplx();
                        break;
                    case "Set...":
                        this.drawSet();
                        break;
                    case "String...":
                        this.drawString();
                        break;
                    case "<- Return":
                        this.categoryview();
                        break;
                    case "Get Help...":
                        try
                        {
                            Program.man.Dispose();
                            Program.man = new HelpDisplay();
                            Program.man.MdiParent = Program.loaded.Parent;
                            //Program.man.showman(cmdtext[0]);
                            Program.man.Show();
                        }
                        catch (CSException ex)
                        {
                            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        break;
                    case "Parameters...":
                        this.drawParamList();
                        break;
                    case "Command History...":
                        Program.ConsoleForm.ShowHistory();
                        this.Close();
                        break;
                    case "File...":
                        if (complete_file.ShowDialog() == DialogResult.OK)
                        {
                            Program.ConsoleForm.AddCmdBoxText(complete_file.FileName);
                            this.Close();
                            Program.ConsoleForm.FocusInput();
                        }
                        break;
                    default:
                        Program.ConsoleForm.AddCmdBoxText(param_items.SelectedItems[0].Text);
                        this.Close();
                        Program.ConsoleForm.FocusInput();
                        break;
                }
            }
        }


        private void paramcomp_KeyDown(object sender, KeyEventArgs e)
        {
            this.param_items.Focus();
            switch (e.KeyCode)
            {
                case Keys.Up:
                case Keys.Down:
                    break;
                case Keys.Enter:
                    this.ActivateItem();
                    break;
                default:
                    if (this.Visible) this.Close();
                    break;
            }
        }

        private void param_items_DoubleClick(object sender, EventArgs e)
        {
            this.ActivateItem();
        }
    }
}
